/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */
import type { Address_Type } from '@/components/KbAddressSelect/index.d';
import { isArray, uniqBy } from 'lodash';

const formatAddress = (list: Address_Type[] = []) => {
  const { province, city } = list[0] || {};
  return `${province}${city}(${list.map((item) => item.district).toString()})`;
};

export function formatAgency(data?: string) {
  if (!data) return '-';
  const s: any[] = [];
  const a: Address_Type[] = JSON.parse(data);
  if (!isArray(a)) return '-';
  uniqBy(a, 'city').forEach((item) => {
    s.push(formatAddress(a.filter((item1) => item1.city === item.city)));
  });
  return s;
}
